import { reqGoodsInfo } from "@/api";
import { reqAddOrUpdateShopCart } from "@/api";
import {shopCart} from "@/api"
// 封装游客身份模块uuid----->生成一个随机字符串(不能在变了)
import {getUUID} from "@/utils/uuid_toKen"
import {deletCartlist} from "@/api"
import {reqUpdateCheckedByid } from "@/api"
const state = {
    goodInfo: {},
    uuid_toKen: getUUID(),
    CartList: []
}
const mutations = {
    GETGOODINFO(state,goodInfo) {
        state.goodInfo = goodInfo
    },
    GETCARLIST(state,carList) {
        state.CartList = carList
    }
};
const actions = {
    // 获取产品消息的actions
    async getGoodInfo({commit},skuId ) {
        let result = await reqGoodsInfo(skuId);
        if(result.code == 200) {
            commit('GETGOODINFO',result.data)
            console.log('getGoodInfo');
        }   
    },
    // 将产品添加到购物车中
    async getUpdateShopCart({commit},{skuId,skuNum } ) {
        // 加入购物车返回的解构
        // 加入购物车以后(发请求),前台将参数带给服务器
        // 服务器写入数据成功，并没有返回其他的数据，只是返回code=200，代表这次操作成功
        // 因为服务器没有返回其余数据，因此不需要三连环存储数据
        let result = await reqAddOrUpdateShopCart(skuId,skuNum);
        console.log(skuId,skuNum);
        // console.log(result);  
       if(result.code==200) {
            return 'ok'
       } else {
        return Promise.reject(new Error('faile'))
       }
     
    },
    // 获取购物车列表信息
    async getCartList({commit}) {
        let result = await shopCart();
   
        if(result.code == 200) {
            commit('GETCARLIST',result.data)
            console.log(result);
        }   
    },
    // 删除购物车的列表信息
    async getdeletCartlist({commit},{skuId}) {
        console.log(skuId,'skuId');
        let result = await deletCartlist(skuId)
       
        if(result.code == 200) {
               return 'ok'
        } else {
            return Promise.reject(new Error('faile'))
        }
    },
    // 获取产品勾选状态
    async getUpdateChecked({commit},{skuId,checked}) {
        console.log(skuId,checked);
        let result = await reqUpdateCheckedByid(skuId,checked)
        if(result.code == 200) {
            return 'ok'
     } else {
         return Promise.reject(new Error('faile'))
     }
    }
};
const getters = {
    categoryView() {
        return state.goodInfo.categoryView || {}
    },
    skuInfo() {
        return state.goodInfo.skuInfo || {}
    },
    carList(state) {
        return state.CartList[0] || {}
    }
};
export default{
    state,
    mutations,
    actions,
    getters
}